/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.ui;

import id.duapuluhdua.dakidaki.Command;
import id.duapuluhdua.dakidaki.R;
import id.duapuluhdua.dakidaki.Utils;
import id.duapuluhdua.dakidaki.activities.ActivityMain;
import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.map.Screen;

/**
 * Displays digits of the current altitude.
 */
public class DigitDisplayAltitude extends DigitDisplay
{
  /** Constants */
  private final static int MAX_ALTITUDE = 9999;

  /** Fields */
  private String mTypeAltitude;

  /**
   * @param parent
   * @param screen
   */
  public DigitDisplayAltitude(CommonActivity parent, Screen screen)
  {
    super(parent, screen, "0000", "ww", R.string.display_altitude);

    mTypeAltitude = parent.getString(R.string.display_altitude);

    this.bAlwaysVisible = true;
  }

  @Override
  public void surfaceSizeChanged(int width, int height, int screenOrientation)
  {
    super.surfaceSizeChanged(width, height, screenOrientation);

    // Set position
    final int posX = width - mRtBounds.width() - (int)mStyle.backgroundMargin;
    setPos(posX, (int)mStyle.backgroundMargin);
  }

  @Override
  public void updateStyle()
  {
    super.updateStyle();

    mStyle.rtBackgroundMarginCorrect.right = 12;
  }

  @Override
  public String getZeroValue()
  {
    return "-";
  }

  public boolean setAltitude(int altitude)
  {
    if (altitude < 0) {
      return false;
    }
    if (altitude > MAX_ALTITUDE) {
      return false;
    }
    String value = Utils.getPreciseDistanceValue(mPreference, altitude);
    setValue(value, Utils.getDistanceUnit(mPreference, altitude));

    return true;
  }

  @Override
  public boolean updateValues()
  {
    final int currentUiMode = mState.getCurrentUiMode();

    // display mode TRACKS
    if (currentUiMode == Command.CMD_MODE_TRACKS) {
      final int altitude = ActivityMain.loader.mStats.getAltitude();
      if (altitude != -1) {
        setType(mTypeAltitude);
        return setAltitude(altitude);
      }
    }
    setType("");
    return false;
  }
}